package com.library.web.controller.api;

import com.library.common.core.controller.BaseController;
import com.library.common.core.page.TableDataInfo;
import com.library.common.utils.DateUtils;
import com.library.domain.BoBorrow;
import com.library.service.IBoBorrowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * @Author julyWhj
 * @Description $
 * @Date 2022/3/27 11:04 下午
 **/
@RestController
@RequestMapping("/api/borrow")
public class BorrowApiController extends BaseController {
    @Autowired
    private IBoBorrowService boBorrowService;

    /**
     * 查询图书借阅列表
     */
    @GetMapping("/list")
    public TableDataInfo list(BoBorrow boBorrow) {
        startPage();
        List<BoBorrow> list = boBorrowService.selectBoBorrowList(boBorrow);
        return getDataTable(change(list));
    }

    private List<Map<String, String>> change(List<BoBorrow> boBorrows) {
        List<Map<String, String>> list = new ArrayList<>(boBorrows.size());
        boBorrows.forEach(boBorrow -> {
            Map<String, String> map = new HashMap<>(16);
            map.put("borrowTime", Objects.nonNull(boBorrow.getBorrowTime()) ? DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, boBorrow.getBorrowTime()) : "--");
            map.put("bookName", Objects.nonNull(boBorrow.getBoBook()) ? boBorrow.getBoBook().getBookName() : "--");
            map.put("bookStatus", "1".equals(boBorrow.getStatus()) ? "已归还" : "未归还");
            map.put("id", String.valueOf(boBorrow.getId()));
            list.add(map);
        });
        return list;
    }
}
